<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="/js/jquery-3.5.1.min.js"></script>
    <script src="/js/popper.min.js"></script>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/bootstrap.min.js"></script>
    <link href="/layui/css/layui.css" rel="stylesheet">
    <script src="/layui/layui.js"></script>
    <link href="/summernote-0.9.0/summernote-bs4.min.css" rel="stylesheet">
    <script src="/summernote-0.9.0/summernote-bs4.min.js"></script>
    <title>添加资讯</title>
    <style>
        body {
            padding: 20px;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .btn-submit {
            margin-top: 20px;
        }
    </style>
</head>
<body>
<div class="container">
    <form id="addNewsForm" method="post">
        <div class="form-group">
            <label for="title">标题</label>
            <input type="text" class="form-control" id="title" name="title" required>
        </div>
        <div class="form-group">
            <label for="content">内容</label>
            <div id="summernote"></div>
        </div>
        <button type="submit" class="btn btn-primary btn-submit">添加文章</button>
    </form>
</div>

<script>
    $(document).ready(function() {
        $('#summernote').summernote({
            placeholder: '请输入内容',
            height: 320,
            width: 680,
            toolbar: [
                ['font', ['bold', 'italic', 'underline', 'clear']],
                ['para', ['ul', 'ol', 'paragraph']],
                ['height', ['height']],
                ['insert', ['picture', 'hr']],
                ['view', ['fullscreen', 'codeview']],
                ['help', ['help']]
            ],
            callbacks: {
                onImageUpload: function(files) {
                    var formData = new FormData();
                    formData.append("file", files[0]);
                    $.ajax({
                        url: "/admins/upload/",
                        data: formData,
                        cache: false,
                        contentType: false,
                        processData: false,
                        type: 'POST',
                        success: function(data) {
                            $('#summernote').summernote('insertImage', data.data);
                        }
                    });
                }
            }
        });
        layui.use('form', function () {
            var form = layui.form;

        });

        $('#addNewsForm').on('submit', function(e) {
            e.preventDefault();
            var title = $('#title').val();
            var content = $('#summernote').summernote('code');

            $.ajax({
                url: '/admins/news/add',
                type: 'POST',
                data: {
                    title: title,
                    content: content
                },
                success: function(response) {
                    if (response.code === 0) {

                        layer.msg("添加成功", {icon: 1, shade: 0.5});
                        setTimeout(function() {
                            // // 刷新父页面的列表
                            // window.parent.location.reload();
                            // // 关闭 iframe
                            // window.parent.closeAddNewsIframe();
                            // 刷新父页面的列表
                            // window.parent.location.reload();
                            //只刷新里面页面
                            window.parent.document.getElementById('addNewsIframe').contentWindow.location.reload();

                        }, 1000);


                        // alert('保存成功');
                        // // 刷新父页面的列表
                        // window.parent.location.reload();
                        // // 关闭 iframe
                        // window.parent.closeAddNewsIframe();
                    } else {
                        alert('保存失败：' + response.msg);
                    }
                },
                error: function() {
                    alert('保存失败，请稍后重试');
                }
            });
        });
    });
</script>
</body>
</html>